package com.Txunda.fxw.ui.fgt;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.Txunda.fxw.R;
import com.Txunda.fxw.base.utils.util.JSONUtils;
import com.Txunda.fxw.base.utils.util.ListUtils;
import com.Txunda.fxw.base.utils.util.MapUtils;
import com.Txunda.fxw.ui.activity.law.CourtCaseAty;
import com.Txunda.fxw.ui.activity.law.Legal_AdviceAty;
import com.Txunda.fxw.ui.activity.law.SingleLawDetailAty;
import com.Txunda.fxw.ui.activity.other.LawServiceAty;
import com.Txunda.fxw.ui.adapter.model.LawDistrict;
import com.Txunda.fxw.ui.base.BaseFgt;
import com.Txunda.fxw.ui.http.Office;
import com.Txunda.fxw.ui.toolkit.util.DensityUtil;
import com.Txunda.fxw.ui.toolkit.util.PreferenceUtil;
import com.Txunda.fxw.ui.widget.XCSlideView;
import com.Txunda.fxw.view.RoundedImageView;
import com.bumptech.glide.Glide;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class LawFgt extends BaseFgt {
    @ViewInject(R.id.aty_title3_tv_center)
    private TextView aty_title3_tv_center;
    @ViewInject(R.id.aty_title3_icon)
    private ImageView aty_title3_icon;


    @ViewInject(R.id.aty_title3_tv_right2)
    private TextView aty_title3_tv_right2;

    private XCSlideView mSlideViewRight;

    private ListView law_area_lv;

    @ViewInject(R.id.law_court_case)
    private ImageView law_court_case;
    @ViewInject(R.id.law_legal_advice)
    private ImageView law_legal_advice;
    @ViewInject(R.id.law_firm)
    private ImageView law_firm;
    @ViewInject(R.id.law_document)
    private ImageView law_document;
    @ViewInject(R.id.law_regulation)
    private ImageView law_regulation;
    @ViewInject(R.id.law_service_solution)
    private ImageView law_service_solution;
    @ViewInject(R.id.img_logo)
    private RoundedImageView img_logo;

    private String district3_id;
    private Office office;
    private ArrayList<Map<String, String>> list_data;

    private ArrayList<LawDistrict> law_district_list;
    private MyAdapter law_firm_adapter;

    private String district_name;
    private ArrayList<LawDistrict> cityList;
    private String district_id;
    private ArrayList<LawDistrict> districtList;

    Map<String, String> map2;
    private String city_name;

    protected int index;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onError(String requestUrl, Map<String, String> error) {
        removeProgressContent();
        removeProgressDialog();
        // haveData = false;
        district_name = cityList.get(index).getProvince_name(); // 地区名称
        district_id = cityList.get(index).getO_id();
        district3_id = cityList.get(index).getProvince_id(); // 三亚传城市
        showToast("已切换至" + district_name);
        aty_title3_tv_right2.setText(district_name);

        if (mSlideViewRight.isShow()) {
            mSlideViewRight.dismiss();
        }
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fgt_law;
    }

    @Override
    @OnClick({R.id.law_court_case, R.id.law_legal_advice, R.id.law_firm, R.id.law_document, R.id.law_regulation,
            R.id.law_service_solution, R.id.img_logo})
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.img_logo: // logo
                showProgressDialog();
                office.areaList(this);
                if (!mSlideViewRight.isShow())
                    mSlideViewRight.show();
                break;
            case R.id.law_court_case: // 法院立案
                Bundle bundle = new Bundle();
                bundle.putString("from", "1");
                startActivity(CourtCaseAty.class, bundle);
                break;
            case R.id.law_legal_advice: // 法律咨询
                bundle = new Bundle();
                bundle.putString("district_id", district_name);
                bundle.putString("city_name", city_name);
                startActivity(Legal_AdviceAty.class, bundle);
                break;
            case R.id.law_firm: // 律师事务所
                bundle = new Bundle();
                bundle.putString("district_name", district_name);
                bundle.putString("district_id", district_id);
                bundle.putString("district_id3", district3_id);
                startActivity(SingleLawDetailAty.class, bundle);
                break;
            case R.id.law_document: // 法律文书
                bundle = new Bundle();
                bundle.putString("from", "4");
                startActivity(CourtCaseAty.class, bundle);
                break;
            case R.id.law_regulation: // 法律法规
                bundle = new Bundle();
                bundle.putString("from", "5");
                startActivity(CourtCaseAty.class, bundle);
                break;
            case R.id.law_service_solution: // 法律客服解答
                bundle = new Bundle();
                Log.d("zdl","=========district_name===="+district_name);
                Log.d("zdl","=========district_id===="+district_id);
                Log.d("zdl","=========district3_id===="+district3_id);
                bundle.putString("district_name", district_name);
                bundle.putString("district_id", district_id);
                bundle.putString("district_id3", district3_id);
                startActivity(LawServiceAty.class, bundle);
                break;

            default:
                break;
        }
    }

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);
        Map<String, String> map = JSONUtils.parseKeyAndValueToMap(jsonStr);
        if (MapUtils.isEmpty(map))
            return;
        if (requestUrl.contains("lawList")) {
            list_data = JSONUtils.parseKeyAndValueToMapList(map.get("data"));
            if (ListUtils.isEmpty(list_data))
                return;
            for (Map<String, String> temp : list_data) {
                switch (Integer.parseInt(temp.get("position"))) {
                    case 1:
                        Glide.with(this).load(temp.get("pic")).into(law_court_case);
                        break;
                    case 2:
                        Glide.with(this).load(temp.get("pic")).into(law_legal_advice);
                        break;
                    case 3:
                        Glide.with(this).load(temp.get("pic")).into(law_firm);
                        break;
                    case 4:
                        Glide.with(this).load(temp.get("pic")).into(law_document);
                        break;
                    case 5:
                        Glide.with(this).load(temp.get("pic")).into(law_regulation);
                        break;
                    case 6:
                        Glide.with(this).load(temp.get("pic")).into(law_service_solution);
                        break;

                    default:
                        break;
                }
            }
        }
        if (requestUrl.contains("areaList")) { // 律师事务所地区选择
            law_district_list.clear();
            List<Map<String, String>> listTemp = JSONUtils.parseKeyAndValueToMapList(map.get("data"));
            if (ListUtils.isEmpty(listTemp))
                return;
            for (Map<String, String> temp : listTemp) {
                law_district_list.add(new LawDistrict(temp.get("o_id"), temp.get("province_id"), temp.get("province_name")));
            }

            law_firm_adapter = new MyAdapter(law_district_list);
            law_area_lv.setAdapter(law_firm_adapter);

            law_area_lv.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    String province_id = law_district_list.get(position).getProvince_id(); // 省级id
                    showProgressDialog();
                    office.areaCity(province_id, LawFgt.this);

                    law_firm_adapter.notifyDataSetChanged();
                    law_district_list.clear();

                }
            });
        }
        if (requestUrl.contains("areaCity")) { // 市级
            cityList.clear();
            List<Map<String, String>> listTemp = JSONUtils.parseKeyAndValueToMapList(map.get("data"));
            if (ListUtils.isEmpty(listTemp))
                return;
            for (Map<String, String> temp : listTemp) {
                cityList.add(new LawDistrict(temp.get("o_id"), temp.get("city_id"), temp.get("city_name")));
            }
            law_firm_adapter = new MyAdapter(cityList);
            law_area_lv.setAdapter(law_firm_adapter);

            law_area_lv.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    // 城市id
                    String city_id = cityList.get(position).getProvince_id();
                    city_name = cityList.get(position).getProvince_name(); // 城市名称
                    showProgressDialog();
                    office.areaDistrict(city_id, LawFgt.this);
                    index = position;

                    law_firm_adapter.notifyDataSetChanged();
                    // 保存本地数据
                    PreferenceUtil.write(getActivity().getApplicationContext(), PreferenceUtil.sp_mode, city_name);
                }
            });

        }
        if (requestUrl.contains("areaDistrict")) {
            map2 = JSONUtils.parseKeyAndValueToMap(jsonStr);
            districtList.clear();
            List<Map<String, String>> listTemp = JSONUtils.parseKeyAndValueToMapList(map2.get("data"));
            if (ListUtils.isEmpty(listTemp))
                return;
            for (Map<String, String> temp : listTemp) {
                districtList
                        .add(new LawDistrict(temp.get("o_id"), temp.get("district_id"), temp.get("district_name")));
            }
            law_firm_adapter = new MyAdapter(districtList);

            law_area_lv.setAdapter(law_firm_adapter);

            law_area_lv.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    // 地区id
                    district_name = districtList.get(position).getProvince_name(); // 地区名称
                    district_id = districtList.get(position).getO_id();
                    district3_id = districtList.get(position).getProvince_id();
                    showToast("已切换至" + district_name);
                    aty_title3_tv_right2.setText(district_name);
                    law_firm_adapter.notifyDataSetChanged();

                    if (mSlideViewRight.isShow()) {
                        mSlideViewRight.dismiss();
                    }
                }
            });

        }
    }

    @Override
    protected void initialized() {
        cityList = new ArrayList<>();
        law_district_list = new ArrayList<>();
        districtList = new ArrayList<>();
        list_data = new ArrayList<>();
        office = new Office();

        Context mContext = getActivity();
        // 屏幕宽度
        int mScreenWidth = DensityUtil.getScreenWidthAndHeight(mContext)[0];
        mSlideViewRight = XCSlideView.create(getActivity(), XCSlideView.Positon.RIGHT);

        View menuViewRight = LayoutInflater.from(mContext).inflate(R.layout.slide_law_area, null);
        mSlideViewRight.setMenuView(getActivity(), menuViewRight);
        mSlideViewRight.setMenuWidth(mScreenWidth * 6 / 9);
        law_area_lv = menuViewRight.findViewById(R.id.law_area_list);

    }

    @Override
    protected void requestData() {
        String logo = PreferenceUtil.readString(getActivity(), PreferenceUtil.sp_logo);
        Glide.with(this).load(logo).into(img_logo);
        String area_name = PreferenceUtil.readString(getActivity().getApplicationContext(), PreferenceUtil.sp_mode);
        aty_title3_tv_center.setText("中国法律服务");
        aty_title3_tv_right2.setVisibility(View.VISIBLE);
        aty_title3_tv_right2.setText(area_name);
        showProgressDialog();
        office.lawList(this);
        aty_title3_icon.setVisibility(View.GONE);
    }

    /**
     * 律师地区筛选adapter
     *
     * @author Administrator
     */
    public class MyAdapter extends BaseAdapter {
        List<LawDistrict> list;
        MyViewHolder vh;

        public MyAdapter(List<LawDistrict> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            int ret = 0;
            if (list != null) {
                ret = list.size();
            }
            return ret;
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(getActivity()).inflate(R.layout.law_item_slide, null);
                vh = new MyViewHolder();
                ViewUtils.inject(vh, convertView);
                convertView.setTag(vh);
            } else {
                vh = (MyViewHolder) convertView.getTag();
            }
            vh.item_slide_law.setText(list.get(position).getProvince_name()); // 地区名称

            return convertView;
        }

        class MyViewHolder {
            @ViewInject(R.id.item_slide_law)
            private TextView item_slide_law;
        }
    }
}
